package com.one.efaimaly.main.ui.fragment;

import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.one.common.config.CMemoryData;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.imageloader.ILoader;
import com.one.common.manager.imageloader.LoaderManager;
import com.one.common.model.bean.UserInfoBean;
import com.one.common.utils.StringUtils;
import com.one.common.view.base.BaseFragment;
import com.one.efaimaly.R;
import com.one.common.model.bean.UserWorkerBean;
import com.one.efaimaly.user.presenter.UserInfoPresenter;
import com.one.efaimaly.user.view.UserInfoView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/5 上午9:21
 * description ：我的
 **/
public class MyFragment extends BaseFragment<UserInfoPresenter> implements UserInfoView {
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.rb_star)
    RatingBar rbStar;
    private UserInfoBean userInfoBean;

    @Override
    protected void initPresenter() {
        mPresenter = new UserInfoPresenter(this, getActivity());
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_my;
    }

    @Override
    public void initView() {
        super.initView();
        userInfoBean = CMemoryData.getUserInfo();
        setUserInfo();

    }

    /**
     * 设置用户信息
     */
    private void setUserInfo() {
        if (userInfoBean != null) {
            if (StringUtils.isNotBlank(userInfoBean.getMember_img())) {
                ILoader.Options options = new ILoader.Options(R.mipmap.ic_avatar_default, R.mipmap.ic_avatar_default, true);
                LoaderManager.getLoader().loadNet(ivAvatar, userInfoBean.getMember_img(), options);
            }
            tvName.setText(userInfoBean.getMember_real_name());
            tvPhone.setText(userInfoBean.getMember_phone());
            tvNum.setText("服务次数：" + userInfoBean.getOrder_count());

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (userInfoBean != null) {
            mPresenter.getUserInfo();
        }
    }

    @Override
    public void setUserInfoView(UserInfoBean bean) {
        if (bean != null) {
            userInfoBean = bean;
            setUserInfo();
        }
    }

    @Override
    public void setUserWorkerView(UserWorkerBean bean) {
        if(bean!= null){
            rbStar.setRating(StringUtils.getFloatToString(bean.getAssessment_star()));
        }
    }

    @OnClick(R.id.hmi_user_info)
    public void toUserInfo() {
        RouterManager.getInstance().go(RouterPath.USER_INFO);
    }

    @OnClick(R.id.hmi_about)
    public void toAbout() {
        RouterManager.getInstance().go(RouterPath.ABOUT);
    }

    @OnClick(R.id.hmi_feedback)
    public void toFeedback() {
        RouterManager.getInstance().go(RouterPath.FEEDBACK);
    }

    @OnClick(R.id.hmi_setting)
    public void toSetting() {
        RouterManager.getInstance().go(RouterPath.SETTING);
    }
}
